package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
public class RabbitController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String sendMsg(@RequestParam(required = false,defaultValue = "10") Integer num) {

        for (int i = 0; i < num; i++) {
            if (i % 2 == 0) {
                OrderReturnReasonEntity returnReasonEntity = new OrderReturnReasonEntity();
                returnReasonEntity.setId(1L);
                returnReasonEntity.setName("哈哈-" + i);
                returnReasonEntity.setCreateTime(new Date());
                // 1、如果发送的消息是个对象，
                // 默认会使用序列化机制写出去，要求对象必须实现 Serializable 接口
                // 可以配置，使用json方式 Jackson2JsonMessageConverter 的 MessageConverter 来替换默认序列化机制
                //
                rabbitTemplate.convertAndSend(
                        "hello-java-exchange",
                        "hello.java",
                        returnReasonEntity,
                        new CorrelationData(UUID.randomUUID().toString())
                );
                log.info("消息发送成功：{}", returnReasonEntity);
            } else {
                OrderEntity orderEntity = new OrderEntity();
                // 1、如果发送的消息是个对象，
                // 默认会使用序列化机制写出去，要求对象必须实现 Serializable 接口
                // 可以配置，使用json方式 Jackson2JsonMessageConverter 的 MessageConverter 来替换默认序列化机制
                //
                rabbitTemplate.convertAndSend(
                        "hello-java-exchange",
                        "hello2.java",
                        orderEntity,
                        new CorrelationData(UUID.randomUUID().toString())
                );
                log.info("消息发送成功：{}", orderEntity);
            }

        }
        return "ok";
    }
}
